package com.svs.system.service;

import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.svs.common.constant.SmsConstants;
import com.svs.framework.base.service.CrudService;
import com.svs.framework.util.RandomUtil;
import com.svs.framework.util.SmsRequestApi;
import com.svs.framework.web.exception.base.BaseException;
import com.svs.system.domain.SmsRecord;
import com.svs.system.mapper.master.SmsRecordDao;

/**
 * 短信发送记录 服务层实现
 * 
 * @author 杜建桥
 * @date 2019-11-06
 */
@Service
public class SmsRecordService extends CrudService<SmsRecordDao, SmsRecord> {
	
	@Autowired
	private SmsRecordDao smsRecordDao;
	
	
	public int sendLoginCode(String mobile){
		//获取该手机最近一次登录短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_LOGIN);//登录类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果存在，则判断是否超过发送时间间隔
		if(null!=last){
			Date date = last.getCreateTime();
			Date now = new Date();
			long interval = (now.getTime() - date.getTime())/1000;
			if(interval<SmsConstants.SMS_REGISTER_SEND_TIME){
				throw new BaseException("请稍后再发送");
			}
		}
		//判断当天发送短信数量
		Calendar cal = Calendar.getInstance();
		record.setCreateTime(cal.getTime());//当天
		int count = smsRecordDao.countSendNum(record);
		if(count>=SmsConstants.SMS_REGISTER_NUM){
			throw new BaseException("当天短信发送数量已达上限"+SmsConstants.SMS_REGISTER_NUM);
		}
		//调用发送短信接口
		String verifyCode = RandomUtil.randStringDigit(SmsConstants.SMS_REGISTER_CODE_LENGTH);
		String[] datas = new String[] { verifyCode };
		//boolean sendFlag = true;
		//boolean sendFlag = MobileSMSUtils.sendDSMS4Reg(mobile,verifyCode );
		boolean sendFlag = SmsRequestApi.getApi().sendSms(mobile, SmsRequestApi.register_template_id, datas);
		if(!sendFlag){
			throw new BaseException("短信发送失败");
		}
		record.setSendFlag(SmsConstants.SMS_SEND_SUCCESS);
		record.setCode(verifyCode);
		cal.add(Calendar.MINUTE, SmsConstants.SMS_REGISTER_INVALID_TIME);
		record.setInvalidTime(cal.getTime());//过期时间
		smsRecordDao.insertSelective(record);
		return record.getSendFlag();
	}
	
	public int sendRegisterCode(String mobile){
		//获取该手机最近一次注册短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_REGISTER);//注册类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果存在，则判断是否超过发送时间间隔
		if(null!=last){
			Date date = last.getCreateTime();
			Date now = new Date();
			long interval = (now.getTime() - date.getTime())/1000;
			if(interval<SmsConstants.SMS_REGISTER_SEND_TIME){
				throw new BaseException("请稍后再发送");
			}
		}
		//判断当天发送短信数量
		Calendar cal = Calendar.getInstance();
		record.setCreateTime(cal.getTime());//当天
		int count = smsRecordDao.countSendNum(record);
		if(count>=SmsConstants.SMS_REGISTER_NUM){
			throw new BaseException("当天短信发送数量已达上限"+SmsConstants.SMS_REGISTER_NUM);
		}
		//调用发送短信接口
		String verifyCode = RandomUtil.randStringDigit(SmsConstants.SMS_REGISTER_CODE_LENGTH);
		String[] datas = new String[] { verifyCode };
		//boolean sendFlag = true;
		//boolean sendFlag = MobileSMSUtils.sendDSMS4Reg(mobile,verifyCode );
		boolean sendFlag = SmsRequestApi.getApi().sendSms(mobile, SmsRequestApi.register_template_id, datas);
		if(!sendFlag){
			throw new BaseException("短信发送失败");
		}
		record.setSendFlag(SmsConstants.SMS_SEND_SUCCESS);
		record.setCode(verifyCode);
		cal.add(Calendar.MINUTE, SmsConstants.SMS_REGISTER_INVALID_TIME);
		record.setInvalidTime(cal.getTime());//过期时间
		smsRecordDao.insertSelective(record);
		return record.getSendFlag();
	}
	
	//发送修改密码验证码
	public int sendPasswdCode(String mobile){
		//获取该手机最近一次注册短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_PASSWD);//修改密码类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果存在，则判断是否超过发送时间间隔
		if(null!=last){
			Date date = last.getCreateTime();
			Date now = new Date();
			long interval = (now.getTime() - date.getTime())/1000;
			if(interval<SmsConstants.SMS_REGISTER_SEND_TIME){//跟注册一样的时间控制
				throw new BaseException("请稍后再发送");
			}
		}
		//判断当天发送短信数量
		Calendar cal = Calendar.getInstance();
		record.setCreateTime(cal.getTime());//当天
		int count = smsRecordDao.countSendNum(record);
		if(count>=SmsConstants.SMS_REGISTER_NUM){//跟注册一样的数量控制
			throw new BaseException("当天短信发送数量已达上限"+SmsConstants.SMS_REGISTER_NUM);
		}
		//调用发送短信接口
		String verifyCode = RandomUtil.randStringDigit(SmsConstants.SMS_REGISTER_CODE_LENGTH);
		String[] datas = new String[] { verifyCode };
		//boolean sendFlag = true;
		//boolean sendFlag = MobileSMSUtils.sendDSMS4Passwd(mobile,verifyCode);
		boolean sendFlag = SmsRequestApi.getApi().sendSms(mobile, SmsRequestApi.passwd_template_id, datas);
		if(!sendFlag){
			throw new BaseException("短信发送失败");
		}
		record.setSendFlag(SmsConstants.SMS_SEND_SUCCESS);
		record.setCode(verifyCode);
		cal.add(Calendar.MINUTE, SmsConstants.SMS_REGISTER_INVALID_TIME);
		record.setInvalidTime(cal.getTime());//过期时间
		smsRecordDao.insertSelective(record);
		return record.getSendFlag();
	}
	
	//发送企业市审核通过短信通知
	public int sendEnterPassMsg(String mobile){
		//获取该手机最近一次注册短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_ENTER_PASS);//市审核通过类型
		//调用发送短信接口
		String verifyCode = RandomUtil.randStringDigit(SmsConstants.SMS_REGISTER_CODE_LENGTH);
		String[] datas = new String[] { verifyCode };
		//boolean sendFlag = true;
		//boolean sendFlag = MobileSMSUtils.sendDSMS4Audit(mobile);
		boolean sendFlag = SmsRequestApi.getApi().sendSms(mobile, SmsRequestApi.enter_pass_template_id, datas);
		if(!sendFlag){
			throw new BaseException("短信发送失败");
		}
		record.setSendFlag(SmsConstants.SMS_SEND_SUCCESS);
		record.setCode(verifyCode);
		smsRecordDao.insertSelective(record);
		return record.getSendFlag();
	}
	
	public boolean checkLoginCode(String mobile,String code){
		//获取该手机最近一次登录短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_LOGIN);//登录类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果不存在 或者无效，则直接失败
		if(null==last || last.getState()!=SmsConstants.SMS_STATE_EFFECTIVE){
			throw new BaseException("注册码失效");
		}
		//判断验证码是否一致
		if(!last.getCode().equals(code)){
			throw new BaseException("请填写正确的注册码");
		}
		//判断验证码是否过期
		Calendar cal = Calendar.getInstance();
		if(null!=last.getInvalidTime() && last.getInvalidTime().before(cal.getTime())){
			throw new BaseException("注册码已过期");
		}
		last.setModifyTime(cal.getTime());
		last.setState(SmsConstants.SMS_STATE_INVALID);
		smsRecordDao.updateByPrimaryKeySelective(last);
		return true;
	}
	
	
	public boolean checkRegisterCode(String mobile,String code){
		//获取该手机最近一次注册短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_REGISTER);//注册类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果不存在 或者无效，则直接失败
		if(null==last || last.getState()!=SmsConstants.SMS_STATE_EFFECTIVE){
			throw new BaseException("注册码失效");
		}
		//判断验证码是否一致
		if(!last.getCode().equals(code)){
			throw new BaseException("请填写正确的注册码");
		}
		//判断验证码是否过期
		Calendar cal = Calendar.getInstance();
		if(null!=last.getInvalidTime() && last.getInvalidTime().before(cal.getTime())){
			throw new BaseException("注册码已过期");
		}
		last.setModifyTime(cal.getTime());
		last.setState(SmsConstants.SMS_STATE_INVALID);
		smsRecordDao.updateByPrimaryKeySelective(last);
		return true;
	}
	
	public boolean checkPasswdCode(String mobile,String code){
		//获取该手机最近一次注册短信发送记录
		SmsRecord record = new SmsRecord();
		record.setMobile(mobile);//手机号
		record.setType(SmsConstants.SMS_TYPE_PASSWD);//注册类型
		SmsRecord last = smsRecordDao.selectLast(record);
		//如果不存在 或者无效，则直接失败
		if(null==last || last.getState()!=SmsConstants.SMS_STATE_EFFECTIVE){
			throw new BaseException("验证码失效");
		}
		//判断验证码是否一致
		if(!last.getCode().equals(code)){
			throw new BaseException("请填写正确的验证码");
		}
		//判断验证码是否过期
		Calendar cal = Calendar.getInstance();
		if(null!=last.getInvalidTime() && last.getInvalidTime().before(cal.getTime())){
			throw new BaseException("验证码已过期");
		}
		last.setModifyTime(cal.getTime());
		last.setState(SmsConstants.SMS_STATE_INVALID);
		smsRecordDao.updateByPrimaryKeySelective(last);
		return true;
	}

}
